//
//  ShopHomePageViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/9.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "ShopHomePageViewController.h"
#import "ShopNewGoodCollectionViewCell.h"

@interface ShopHomePageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, assign)NSInteger picWidth;

@property (nonatomic, assign)NSInteger picHeight;

@property (nonatomic, assign)NSInteger picNum;

@property (nonatomic, retain)NSTimer *timer;

@property (nonatomic, strong)NSArray *array;//UICollectionView里面的数据
@end

@implementation ShopHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.picWidth=375;
    self.picHeight=200;
    self.picNum=5;
    
    for (int i=0; i<self.picNum; i++) {
        CGFloat px=i*self.picWidth;
        
        NSString *path=[NSString stringWithFormat:@"%d.jpg",i+1];
        
        UIImageView *pic=[[UIImageView alloc]initWithFrame:CGRectMake(px, 0, self.picWidth, self.picHeight)];
        
        pic.image=[UIImage imageNamed:path];
        
        [self.scrollerView addSubview:pic];
        
    }
    
    self.scrollerView.contentSize=CGSizeMake(self.picNum*self.picWidth, 0);
    
    self.scrollerView.pagingEnabled=YES;
    
    self.pageControl.numberOfPages=self.picNum;
    
    [self startTimer];
    
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

/**
 *  array的懒加载
 *
 *  @return NSArray
 */
- (NSArray *)array
{
    if (!_array) {
        _array = @[@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"},@{@"image":@"logo",@"name":@"iphone6s土豪金",@"money":@"￥5178.00",@"amount":@"2000"}];
    }
    return _array;
}

/**
 *  开启定时器
 */
- (void)startTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changePic) userInfo:@"" repeats:YES];
}
/**
 *  定时器每秒触发 切换图片
 */
- (void)changePic{
    NSInteger index=self.pageControl.currentPage;
    
    if (index==self.picNum-1) {
        //        self.scrollerView.contentOffset=CGPointZero;
        [self.scrollerView setContentOffset:CGPointZero animated:YES];
        
    }
    else {
        index++;
        //self.scrollerView.contentOffset=CGPointMake(self.picWidth*index,0);
        
        [self.scrollerView setContentOffset: CGPointMake(self.picWidth*index,0)animated:YES];//添加动画
    }
    
}
/**
 *  移除定时器
 */
- (void)removeMyTimer{
    if (self.timer!=nil) {
        [self.timer invalidate];
        self.timer=nil;
    }
}
/**
 *  滑动scorllView触发
 *
 *
 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width=scrollView.contentOffset.x;
    
    self.pageControl.currentPage=width/self.picWidth;
    
}

/**
 *  开始拖拽时移除定时器
 *
 *  @param scrollView
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeMyTimer];
}
/**
 *
 *停止拖拽时启动定时器
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}


/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 指定section中的collectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

// 配置section中的collectionViewCell的显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ShopNewGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.dic = self.array[indexPath.row];
    return cell;
}

@end
